<?php
class GameRequest{
	public $SenderID = -1;
	public $GameID = -1;
	public $Type = 0;
	public $Message = array();
	static function swap($bytebuffer){
		$obj = new GameRequest(DataUtil::readInt32( $bytebuffer ) ,DataUtil::readInt32( $bytebuffer ) ,DataUtil::readInt8( $bytebuffer ) ,implode(array_map("chr",  DataUtil::readSBin( $bytebuffer )  )) );
		return $obj;
	}
	function __construct($senderid,$gameid,$type,$message){
		$this->SenderID = $senderid;
		$this->GameID = $gameid;
		$this->Type = $type;
		$this->Message = unpack('C*', $message ) ;
	}
	function getMessage(){
		return implode(array_map("chr",  $this->Message )) ;
	}
	function getBinSize(){
		$size = 0;
		$size += 4 ; 
		$size += 4 ; 
		$size += 1 ; 
		$size += 2 ; 
		if(is_array($this->Message)){
			$size += count($this->Message);
		}
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeInt32( $buf , $this->SenderID ) ;
		DataUtil::writeInt32( $buf , $this->GameID ) ;
		DataUtil::writeInt8( $buf , $this->Type ) ;
		DataUtil::writeSBin( $buf , $this->Message ) ;
		return  $buf ->_array() ;
	}
}
class DataUserRequests{
	private $Revision;
	private $RequestsList = array();
	function __construct(){
		$this->Revision = 0;
	}
	static function swap($bytebuffer){
		$obj = new DataUserRequests();
		$obj->Revision = DataUtil::readInt32( $bytebuffer ) ;
		$num_of_request = DataUtil::readInt32( $bytebuffer ) ;
		for($i = 0; $i < $num_of_request; $i++){
			$obj->RequestsList[] = GameRequest::swap($bytebuffer);
		}
		return $obj;
	}
	function getRevision(){
		return $this->Revision;
	}
	function getRequests(){
		return $this->RequestsList;
	}
	function addRequest($senderid,$gameid,$type,$message){
		$this->Revision = ($this->Revision+1)%50000;
		$this->RequestsList[] = new GameRequest($senderid,$gameid,$type,$message);
	}
	function removeRequest($senderid,$gameid,$type){
		if(is_array($this->RequestsList)){
			foreach($this->RequestsList as $key=>$request){
				if($request->SenderID == $senderid && $request->GameID == $gameid && $request->Type == $type){
					unset($this->RequestsList[$key]);
					$this->RequestsList = array_values($this->RequestsList);
					$this->Revision = ($this->Revision+1)%50000;
					return true;
				}
			}
		}
		return false;
	}
	function getBinSize(){
		$size = 0;
		$size += 4 ; 
		$size += 4 ; 
		if(is_array($this->RequestsList)){
			foreach($this->RequestsList as $request){
				$size += $request->getBinSize();
			}
		}
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeInt32( $buf , $this->Revision ) ;
		DataUtil::writeInt32( $buf , count($this->RequestsList) ) ;
		if(is_array($this->RequestsList)){
			foreach($this->RequestsList as $request){
				DataUtil::writeBytes( $buf , $request->getBinary() ) ;
			}
		}
		return  $buf ->_array() ;
	}
}
?>